-- ================================================
-- Template generated from Template Explorer using:
-- Create Trigger (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- See additional Create Trigger templates for more
-- examples of different Trigger statements.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Stefan Richter
-- Create date: 
-- Description:	
-- =============================================
CREATE TRIGGER [dbo].FillAdGroup 
   ON  [ITSSP].[dbo].[UserSet]
   AFTER INSERT,DELETE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	-- Insert statements for trigger here
	
	-- Get ID of AD role
	DECLARE @ADid INTEGER
	SELECT @ADid = (SELECT Id FROM RoleSet WHERE Name='AD')

	-- Remove old AD role associations
	DELETE FROM UserRole WHERE Roles_Id=@ADid
	
	-- Insert new associations
	INSERT INTO UserRole SELECT Id as User_Id, @ADid as Roles_Id from UserSet where ActiveDirectoryUser=1 and not Id in (Select Id from deleted)
END
GO
